<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>听说你要翻译</title>
    <link rel="shortcut icon" type="image/x-icon" href="./favicon.ico" />
    <style>
        body{
            background-color:#f9f9f9;
        }
        .main-container{
            border:1px solid #ccc;
            width:1200px;
            height:500px;
            margin:50px auto auto auto;
        }
        .select-container{
            border:1px solid #ccc;
            width:1200px;
            height:150px;
        }
        .translate-container{
            border:1px solid #ccc;
            width:1200px;
            height:400px;
        }
        .translate-form{
            border:1px solid green;
        }
        textarea{
            width:590px;
            height:200px;
            margin-top:50px;
            font-size:20px;
        }
        .select{
            min-width:100px;
            height:25px;
            text-align-all: center;
        }
        .confirm-btn{
            background-color: #4395ff;
            height:25px;
            width:80px;
            color:white;
        }
    </style>
    <script src="./js/jquery-3.1.0.min.js"></script>
    <script>
        function getLanguageList() {
            $.get("/trans/languages", null,
                    function(data){
                        var options1="";
                        var options2="";
                        for(var i=0;i<data.length;i++){
                            if(i==0){
                                options1='<option value="'+data[i].shorthand+'" selected>'+data[i].fullName+'</option>';
                                continue;
                            }
                            options1=options1+'<option value="'+data[i].shorthand+'">'+data[i].fullName+'</option>';
                            if(data[i].shorthand=="auto"){
                                continue;
                            }
                            if(i==1){
                                options2='<option value="'+data[i].shorthand+'" selected>'+data[i].fullName+'</option>';
                                continue;
                            }
                            options2=options2+'<option value="'+data[i].shorthand+'">'+data[i].fullName+'</option>';
                        }

                        $("#select-from").html(options1);
                        $("#select-to").html(options2);
                    });
        }
        function translateText() {
            var query=$("#query").val().trim();
            var from=$("#select-from option:selected").val();
            var to=$("#select-to option:selected").val();
            $.get("/trans/translate?query="+query+"&from="+from+"&to="+to+"",function (result) {
                $("#result").val(result);
            })
        }
        function init() {
            getLanguageList();
        }
        window.onload=init();
    </script>
</head>
<body>
<div class="main-container">
    <form class="translate-form">

        <div class="translate-container">
            <textarea rows="20" cols="80" id="query" placeholder="此处输入翻译内容..."></textarea>
            <textarea rows="20" cols="80" id="result" disabled="disabled"></textarea>
        </div>
        <div class="select-container">
            <select name="from" class="select" id="select-from">
                <option value="zh" selected>中文</option>
            </select>
            <span>==></span>
            <select name="to" class="select" id="select-to">
                <option value="en" selected>英文</option>
            </select>
            <input type="button" value="翻 译" class="confirm-btn" onclick="translateText();"/>
        </div>
    </form>
</div>
</body>
</html>